package com.blm.controller.admin;

import com.blm.common.Result;
import com.blm.dto.FileDeleteDTO;
import com.blm.util.MinioUtil;
import com.blm.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理员API - 资源管理", description = "管理系统图片资源")
@RestController
@RequestMapping("/api/admin/resources")
public class AdminResourceController {
    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private SecurityUtil securityUtil;

    @Operation(summary = "上传图片", description = "上传系统内部图片资源")
    @ApiResponse(responseCode = "200", description = "图片上传成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class)))
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> upload(@RequestParam MultipartFile file) {
        Long adminId = securityUtil.getCurrentUserId();
        return Result.success(minioUtil.uploadImages(adminId, file, MinioUtil.ImageType.APP));
    }

    @Operation(summary = "删除url对应的图像")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping
    public Result<Void> deleteAvatar(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "删除的文件对应的url,不包含endpoint 例如：blm-images/preview/avatar/1234567890.jpg",
                    required = true,
                    content = @Content(schema = @Schema(implementation = FileDeleteDTO.class)))
            @org.springframework.web.bind.annotation.RequestBody FileDeleteDTO dto) {
        Long adminId = securityUtil.getCurrentUserId();
        minioUtil.deleteUrlImage(adminId, dto.getUrl());
        return Result.success(null);
    }
}
